package org.njbdqn.mybuy.database;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DbManager {
	private Connection conn;
	private PreparedStatement pstat;
	private ResultSet rs;
	
	public DbManager(){
		conn = DbUtil.getConnection();
	}
	
	public int update(String sql,Object [] params){
		int count=0;
		try {
			pstat=conn.prepareStatement(sql);
			for (int i = 0; i < params.length; i++) {
				pstat.setObject(i+1, params[i]);
			}
			count=pstat.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return count;
	}
	
	public ResultSet query(String sql,Object [] params){
		try {
			pstat=conn.prepareStatement(sql);
			for (int i = 0; i < params.length; i++) {
				pstat.setObject(i+1, params[i]);
			}
			rs=pstat.executeQuery();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return rs;
	}
	
	public void destory(){
		DbUtil.destory(this.conn);
	}
}
